rm(list = ls())

library(kableExtra)
library(tidyverse)

## Function to summarize variable 

summarize_vec <- function(vector){
  
  mean_v <- mean(vector, na.rm = T)
  median_v <- median(vector, na.rm = T)
  sd_v <- sd(vector, na.rm = T)
  n_obs <- length(vector[!is.na(vector)])
  min_v <- min(vector, na.rm = T)
  max_v <- max(vector, na.rm = T)
  
  
  data.frame(cbind(mean_v, median_v, sd_v, n_obs, min_v, max_v))
  
}


## Load MP data

dat <- readRDS('data/mp_data.rds')  %>%
  filter(vote_abstain_no_show == 0) %>%
  filter(elecper != 8) 

## Drop haven label 

dat$gender <- haven::zap_label(dat$gender)

experience_covars <- c('nsdap_member', 
                       'syn_in_gem_bin', 
                       'syn_in_loc_33_39',
                       'n_syn_district',
                       'n_syn_district_bin',
                       'rel_cath',
                       'veteran_ww1',
                       'veteran_ww2', 
                       'capture_ww2',
                       'soviet_capture',
                       'exile_repressed_combined', 
                       'resistance_member')

## Define variables to summarize 

sumvars <- c('year_birth', 'gender', 'mandate', 'closeness_district', 'dualcand', experience_covars)


## Summarize 

dat <- dat %>% 
  dplyr::select(one_of(sumvars)) %>%
  mutate_all(as.numeric) 

summary_all <- apply(dat, 2, summarize_vec) %>%
  reduce(rbind) %>%
  mutate_all(round, 2) %>%
  mutate(label = c('Year of birth',
                   'Male (0/1)',
                   'District mandate (0/1)',
                   'District closeness',
                   'Dual candidate (0/1)',
                   'NSDAP member', 
                   'Synagogue in hometown of MP',
                   'Synagogue in location 1933-1939',
                   'Count of synagogues in electoral district',
                   'Synagogue in electoral district (0/1)',
                   'Catholic',
                   'WW1 Veteran', 
                   'WW2 Veteran',
                   'WW2 POW',
                   'Soviet POW',
                   'Repressed / Exile', 
                   'Resistance')) %>%
  dplyr::select(label, everything())

##

names(summary_all) <- c('Variable', 'Mean', 'Median', 'S.D.', 'N', 'Min', 'Max')

# To Tex Table 

tab_tex <- summary_all %>%
  kableExtra::kable(., format = 'latex', 
                    booktabs = T,
                    linesep = "",
                    label = 'sumstat_mps',
                    caption = 'Summary statistics, roll call voting data',
                    digits = 2) %>%
  footnote(footnote_as_chunk = T, threeparttable = T,
           general = "The table shows summary statistics for the roll call voting data.") %>%
  kable_styling(latex_options = c("HOLD_position"), 
                position = "center",
                full_width = F,
                font_size = 11) 


tab_tex

